﻿using System;
using System.Collections.Generic;
using System.Xml;


namespace WorldOfMUDra
{
    public abstract class XmlLoadedItem : IItem
    {
        protected string Nof;
        protected string name;
        public virtual string Name
        {
            get { return (name + " (" + Id + ")"); }
        }
        protected string description;
        protected string Id;
        public string ID
        {
            get { return Id; }
        }
        virtual public string Description
        {
            get
            {
                return description;
            }
        }
        protected string ListDescription;
        protected bool Pickupable;
        virtual public bool Obtainable
        {
            get { return Pickupable; }
        }
        bool _bound;

        protected Dictionary<string, string> Tags = new Dictionary<string, string>();

        protected XmlLoadedItem(string nameoffile)
        {
            Nof = nameoffile;
            XmlTextReader reader = new XmlTextReader(nameoffile);
            while (!reader.EOF)
            {
                if (reader.NodeType == XmlNodeType.Element)
                {
                    switch (reader.Name.ToLower())
                    {
                        case "name":
                            name = reader.ReadString().Trim();
                            Console.WriteLine("Found item name: " + name);
                            break;
                        case "id":
                            Id = reader.ReadString().Trim();
                            Console.WriteLine("Found item id: " + Id);
                            break;
                        case "listdescription":
                            ListDescription = reader.ReadString().Trim();
                            Console.WriteLine("Found item ld for id: " + Id);
                            break;
                        case "description":
                            description = reader.ReadString().Trim();
                            Console.WriteLine("Found item desc for id: " + Id);
                            break;
                        case "pickupable":
                            if (reader.ReadString().Trim() == "0" || reader.ReadString().Trim().ToLower() == "false")
                            {
                                Pickupable = false;
                            }
                            else
                            {
                                Pickupable = true;
                            }
                            Console.WriteLine("Found picku for item id: " + Id);
                            break;
                        case "bound":
                            if (reader.ReadString().Trim() == "0" || reader.ReadString().Trim().ToLower() == "false")
                            {
                                _bound = false;
                            }
                            else
                            {
                                _bound = true;
                            }
                            Console.WriteLine("Found bound for item id: " + Id);
                            break;
                        default:
                            Tags.Add(reader.Name.ToLower(), reader.ReadString().Trim());
                            Console.WriteLine("Found extra tag for item id: " + Id);
                            break;
                    }
                }
                else { reader.Read(); }
            }
            reader.Close();
        }

        public XmlLoadedItem Copy()
        {
            return (XmlLoadedItem)MemberwiseClone();
        }

        public abstract void OnFirstPickUp(Player p);

        public abstract void OnLook(Player p);

        public abstract void OnPickUp(Player p);
    }
}
